package com.crealth.db;

import java.util.List;

import org.apache.log4j.Logger;

import com.crealth.manager.MybatisManager;
import com.crealth.model.ClienteleInfoModel;
import com.crealth.utils.StringUtil;


public class ClienteleDb {
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static List<ClienteleInfoModel> selectClienteleInfo(long begID, int userID, String commitDataIDs) {
		String cdrTbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
		String cdrPreMonthTbName = "e_cdr_" + StringUtil.getPreMonthString(-1).substring(0, 6);
		List<ClienteleInfoModel> clientls = MybatisManager.clienteleMapper
				.selectClienteleInfo(begID, userID, cdrTbName, cdrPreMonthTbName, commitDataIDs);
		
		return clientls;
	}
	
	public static List<ClienteleInfoModel> selectClienteleInfoSelf(int userID, String commitDataIDs) {
		List<ClienteleInfoModel> clientls = MybatisManager.clienteleMapper
				.selectClienteleInfoSelf(commitDataIDs);
		
		return clientls;
	}
	
	public static List<ClienteleInfoModel> selectClienteleInfoByCreateUser(int userID) {
		List<ClienteleInfoModel> clientls = MybatisManager.clienteleMapper
				.selectClienteleInfoByCreateUser(userID);
		
		return clientls;
	}
	
	public static Integer selectClienteleCntByCondition(String strCondition) {
		Integer res = null;
		try {
			res = MybatisManager.clienteleMapper.selectClienteleCntByCondition(strCondition);
		} catch (Exception e) {
			errorLog.error("", e);
		}
		return res;
	}
	
	public static List<ClienteleInfoModel> selectClienteleInfoByCondition(String strCondition) {
		List<ClienteleInfoModel> clientls = null;
		try {
			clientls = MybatisManager.clienteleMapper
					.selectClienteleInfoByCondition(strCondition);
		} catch (Exception e) {
			errorLog.error("", e);
			return null;
		}
		
		return clientls;
	}
    
    public static void updateDispatchedUserID(Integer setUserID, String inIDs) {
    	MybatisManager.clienteleMapper.updateDispatchedUserID(setUserID, inIDs);
    	MybatisManager.commit();
    }    

    public static void insertClienteleInfo(String strMobi, String strNoti, String strName,
    		Integer createrID) {
    	MybatisManager.clienteleMapper.insertClienteleInfo(strMobi, strNoti, strName, createrID);
    	MybatisManager.commit();
    }
    
    public static Integer insertClienteleObj(ClienteleInfoModel cliModel, boolean needCommit) {
    	Integer res = 0;
    	try {
	    	MybatisManager.clienteleMapper.insertClienteleObj(cliModel);
	    	if (needCommit) {
	    		MybatisManager.commit();
	    	}
    	}
    	catch(Exception e) {
    		errorLog.error("insertClienteleObj", e);
    	}
    	
    	return res;
    }
    
    public static void updateCallClientele(String callMobi, int setUserID) {
    	MybatisManager.clienteleMapper.updateCallClientele(callMobi, setUserID);
    	MybatisManager.commit();
    }
    
    public static List<ClienteleInfoModel> selectClienteleInfoByMobi(String searchMobi) {
    	List<ClienteleInfoModel> clientls = MybatisManager.clienteleMapper.selectClienteleInfoByMobi(searchMobi);
    	
    	return clientls;
    }
    
    public static List<ClienteleInfoModel> selectClienteleInfoFromAlbbByMobi(String searchMobi) {
    	List<ClienteleInfoModel> clientls = MybatisManager.clienteleMapper.selectClienteleInfoFromAlbbByMobi(searchMobi);
    	
    	return clientls;
    }
    
    public static List<ClienteleInfoModel> selectClienteleInfoByName(String searchName) {
    	List<ClienteleInfoModel> clientls = MybatisManager.clienteleMapper.selectClienteleInfoByName(searchName);
    	
    	return clientls;
    }
    
    public static Integer selectMaxClientID() {
    	Integer maxID = MybatisManager.clienteleMapper.selectMaxClientID();
    	
    	return maxID;
    }
    
    public static void resetTodayDispatchNum() {
    	MybatisManager.clienteleMapper.resetTodayDispatchNum();
    	MybatisManager.commit();
    }
    
    public static List<String> selectAllForbidCallee() {
    	try {
    		List<String> phoneNumls = MybatisManager.clienteleMapper.selectAllForbidCallee();
    		return phoneNumls;
    	}
    	catch(Exception e) {
    		errorLog.error("selectAllForbidCallee", e);
    	}
    	return null;
    }
    
    public static Integer updateSelfAlreadyCall(int userID,  String callMobi, int setAlreadyCall) {
    	Integer res = MybatisManager.clienteleMapper.updateSelfAlreadyCall(userID, callMobi, setAlreadyCall);
    	
    	return res;
    }
    
    public static Integer updateSelfAlreadyCallList(int userID, String callMobils, int setAlreadyCall) {
    	Integer res = null;
    	try {
    		res = MybatisManager.clienteleMapper.updateSelfAlreadyCall(userID, callMobils, setAlreadyCall);
    	}
    	catch(Exception e) {
    		errorLog.error("updateSelfAlreadyCallList", e);
    	}
    	return res;
    }
    
    public static List<ClienteleInfoModel> selectClienteleName(int id) {
    	List<ClienteleInfoModel> infols = MybatisManager.clienteleMapper.selectClienteleName(id);
    	
    	return infols;
    }
    public static Integer updateNameByID(int id, String setName) {
    	Integer res = MybatisManager.clienteleMapper.updateNameByID(id, setName);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static List<ClienteleInfoModel> selectClienteleHous(int id) {
    	List<ClienteleInfoModel> infols = MybatisManager.clienteleMapper.selectClienteleHous(id);
    	
    	return infols;
    }
    public static Integer updateHousByID(int id, String setHous) {
    	Integer res = MybatisManager.clienteleMapper.updateHousByID(id, setHous);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer deleteSelfClient(int userID) {
    	Integer res = MybatisManager.clienteleMapper.deleteSelfClient(userID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static void deleteAllClient() {
    	MybatisManager.clienteleMapper.deleteAllClient();
    	MybatisManager.commit();
    	
    }
    
    public static Integer insertGroupCallMobi(String sqlValues) {
    	if (sqlValues == null || sqlValues.length() == 0) {
    		return 0;
    	}
    	Integer res = MybatisManager.clienteleMapper.insertGroupCallMobi(sqlValues);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer deleteAllGroupCallMobi(int companyID) {
    	Integer res = MybatisManager.clienteleMapper.deleteAllGroupCallMobi(companyID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static List<String> selectNotCallMobiPhone(int companyID) {
    	List<String> mobils = MybatisManager.clienteleMapper.selectNotCallMobiPhone(companyID);
    	
    	return mobils;
    }
    
    public static Integer updateDispenseNoCallByUserID(int userID) {
    	Integer res = MybatisManager.clienteleMapper.updateDispenseNoCallByUserID(userID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateCdridByMobiPhone(String mobiPhone, long setCdrid) {
    	Integer res = MybatisManager.clienteleMapper.updateCdridByMobiPhone(mobiPhone, setCdrid);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateGroupCallDispenseUserid(String strMobiPhonels, int setUserID) {
    	Integer res = MybatisManager.clienteleMapper.updateGroupCallDispenseUserid(strMobiPhonels, setUserID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateClearNotCallFromGroupCallMobi() {
    	Integer res = MybatisManager.clienteleMapper.updateClearNotCallFromGroupCallMobi();
    	MybatisManager.commit();
    	
    	return res;
    }
    
    
    
    public static List<ClienteleInfoModel> selectAllClienteleMobi(int smallID) {
    	List<ClienteleInfoModel> clientelels = MybatisManager.clienteleMapper
    			.selectAllClienteleMobi(smallID);
    	
    	return clientelels;
    }
    public static Integer updateClienteMobi(int id, String setMobi) {
    	Integer res = MybatisManager.clienteleMapper.updateClienteMobi(id, setMobi);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static List<String> selectAllGroupCallMobiPhone() {
    	List<String> mobils = MybatisManager.clienteleMapper.selectAllGroupCallMobiPhone();
    	
    	return mobils;
    }
    public static Integer updateGroupCallMobi(String mobi, String setMobi) {
    	Integer res = MybatisManager.clienteleMapper.updateGroupCallMobi(mobi, setMobi);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static List<ClienteleInfoModel> selectAllClienteleMobiFromAlbb(int smallID) {
    	List<ClienteleInfoModel> clientelels = MybatisManager.clienteleMapper
    			.selectAllClienteleMobiFromAlbb(smallID);
    	
    	return clientelels;
    }
    
    public static Integer updateClienteAlbbMobi(int id, String setMobi) {
    	Integer res = MybatisManager.clienteleMapper.updateClienteAlbbMobi(id, setMobi);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer selectGroupCallMobiSumNum() {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiSumNum();    	
    	return res;
    }    
    public static Integer selectGroupCallMobiNotCallNum() {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiNotCallNum();    	
    	return res;
    }    
    public static Integer selectGroupCallMobiAlreadyCallNum() {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiAlreadyCallNum();    	
    	return res;
    }    
    public static Integer selectUserClienteleSumNum(int userID) {
    	Integer res = MybatisManager.clienteleMapper.selectUserClienteleSumNum(userID);    	
    	return res;
    }
    public static Integer selectUserClienteleNotCallNum(int userID) {
    	Integer res = MybatisManager.clienteleMapper.selectUserClienteleNotCallNum(userID);
    	
    	return res;
    }
    
    public static Integer selectGroupCallMobiSumNum2(int companyID) {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiSumNum2(companyID);    	
    	return res;
    }    
    public static Integer selectGroupCallMobiNotCallNum2(int companyID) {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiNotCallNum2(companyID);    	
    	return res;
    }    
    public static Integer selectGroupCallMobiAlreadyCallNum2(int companyID) {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiAlreadyCallNum2(companyID);    	
    	return res;
    }    
    
    public static Integer updateAccCallNumByPhonenum(String mobiPhone) {
    	Integer res = MybatisManager.clienteleMapper.updateAccCallNumByPhonenum(mobiPhone);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer selectGroupCallMobiHold() {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiHold();    	
    	return res;
    }    
    public static Integer selectGroupCallMobiNotDispense() {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiNotDispense();    	
    	return res;
    }
    
    public static Integer selectGroupCallMobiHold2(int companyID) {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiHold2(companyID);    	
    	return res;
    }    
    public static Integer selectGroupCallMobiNotDispense2(int companyID) {
    	Integer res = MybatisManager.clienteleMapper.selectGroupCallMobiNotDispense2(companyID);    	
    	return res;
    }
    
    
}
